package com.novel.authorization.staff.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.novel.authorization.staff.entity.NfTenant;
import com.novel.authorization.staff.service.NfTenantService;
import com.novel.base.entity.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 租户表
 *
 * @author 樊温军
 * @date Wed May 07 11:00:58 CST 2025
 */
@Slf4j
@Api(tags = "租户表", value = "租户表")
@RestController
@AllArgsConstructor
@RequestMapping("/nfTenant")
public class NfTenantController {

    private final NfTenantService nfTenantService;

    /**
     * 租户表—正常列表
     *
     * @return R<List < NfTenant>>
     */
    @SaIgnore
    @ApiOperation(value = "租户表—正常列表", notes = "租户表—正常列表")
    @GetMapping("/normal")
    public R<List<NfTenant>> getNormalTenant() {
        try {
            return R.ok(nfTenantService.getNormalTenant(), "查询成功");
        } catch (Exception e) {
            log.error("查询租户表—列表时发生错误：{}", e.getMessage(), e);
            return R.failed("查询租户表—列表时发生错误，请稍后重试！");
        }
    }

    /**
     * 租户表—列表
     *
     * @param nfTenant 租户表
     * @return R<List < NfTenant>>
     */
    @SaCheckPermission("nfTenant:list")
    @ApiOperation(value = "租户表—列表", notes = "租户表—列表")
    @GetMapping("/list")
    public R<List<NfTenant>> getNfTenantList(NfTenant nfTenant) {
        log.info("开始查询租户表—列表，参数：{}", nfTenant);
        try {
            return R.ok(nfTenantService.getNfTenantList(nfTenant), "查询成功");
        } catch (Exception e) {
            log.error("查询租户表—列表时发生错误：{}", e.getMessage(), e);
            return R.failed("查询租户表—列表时发生错误，请稍后重试！");
        }
    }

    /**
     * 租户表—列表—分页
     *
     * @param page     分页对象
     * @param nfTenant 租户表
     * @return R<IPage < NfTenant>>
     */
    @SaCheckPermission("nfTenant:page")
    @ApiOperation(value = "租户表—列表—分页", notes = "租户表—列表—分页")
    @GetMapping(value = "/page")
    public R<IPage<NfTenant>> getNfTenantPage(Page<NfTenant> page, NfTenant nfTenant) {
        log.info("开始查询租户表—列表—分页，第 {} 页，每页 {} 条记录，其他参数：{}", page.getCurrent(), page.getSize(), nfTenant);
        try {
            IPage<NfTenant> iPage = nfTenantService.getPage(page, nfTenant);
            return R.ok(iPage, "查询成功");
        } catch (Exception e) {
            log.error("查询租户表—列表—分页时发生错误：{}", e.getMessage(), e);
            return R.failed("查询租户表—列表—分页时发生错误，请稍后重试！");
        }
    }

    /**
     * 租户表—保存
     *
     * @param nfTenant 租户表
     * @return R<Long>
     */
    @SaCheckPermission("nfTenant:saveOne")
    @ApiOperation(value = "租户表—保存", notes = "租户表—保存")
    @PostMapping(value = "/saveOne")
    public R<Long> saveOneNfTenant(@Valid @RequestBody NfTenant nfTenant) {
        log.info("开始保存租户表：{}", nfTenant);
        try {
            Long id = nfTenantService.saveOneNfTenant(nfTenant);
            log.info("租户表保存成功，ID: {}", id);
            return R.ok(id, "保存成功");
        } catch (Exception e) {
            log.error("保存租户表时发生错误：{}", e.getMessage(), e);
            return R.failed("保存租户表时发生错误，请稍后重试！");
        }
    }

    /**
     * 租户表—删除
     *
     * @param id 租户表主键
     * @return R<Boolean>
     */
    @SaCheckPermission("nfTenant:delById")
    @ApiOperation(value = "租户表—删除", notes = "租户表—删除")
    @PostMapping(value = "/delById")
    public R<Boolean> delByIdNfTenant(Long id) {
        log.info("开始删除租户表：{}", id);
        if (id == null || id <= 0) {
            log.warn("ID为空或无效: {}", id);
            return R.failed("请传入有效的ID！");
        }
        try {
            nfTenantService.delById(id);
            return R.ok(Boolean.TRUE, "删除成功");
        } catch (Exception e) {
            log.error("删除租户表失败: ID={}, Error={}", id, e.getMessage(), e);
            return R.failed("删除失败，请稍后重试！");
        }
    }

    /**
     * 租户表—详情
     *
     * @param id 租户表主键
     * @return R<NfTenant>
     */
    @SaCheckPermission("nfTenant:details")
    @ApiOperation(value = "租户表—详情", notes = "租户表—详情")
    @GetMapping(value = "/details")
    public R<NfTenant> getNfTenant(Long id) {
        log.info("开始查询租户表：{}", id);
        if (id == null || id <= 0) {
            log.warn("ID为空或无效: {}", id);
            return R.failed("请传入有效的ID！");
        }
        try {
            NfTenant nfTenant = nfTenantService.queryById(id);
            if (nfTenant != null) {
                return R.ok(nfTenant, "查询成功");
            } else {
                return R.failed("未找到租户表");
            }
        } catch (Exception e) {
            log.error("查询租户表详情时发生错误：{}", e.getMessage(), e);
            return R.failed("查询租户表详情时发生错误，请稍后重试！");
        }
    }
}
